package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Category;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        // 转换为 entity 对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // 设置新增分类的状态
        category.setStatus(StatusConstant.ENABLE);

        categoryMapper.insert(category);
    }

    /**
     * 分类管理分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageDTO<Category> page(CategoryPageQueryDTO queryDTO) {
        // 设置分页参数
        Page<Category> page = Page.of(queryDTO.getPage(), queryDTO.getPageSize());
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(null != queryDTO.getName(), Category::getName, queryDTO.getName())
                .eq(null != queryDTO.getType(), Category::getType, queryDTO.getType());
        wrapper.orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime);
        // 分页查询
        page(page, wrapper);
        // 提取结果
        List<Category> records = page.getRecords();
        long total = page.getTotal();

        return new PageDTO<>(total, records);
    }

    /**
     * 根据id修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        // 转换为 entity 对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.update(category);
    }

    /**
     * 根据分类id查询
     * @param id
     * @return
     */
    @Override
    public Category getById(long id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        // 设置查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, StatusConstant.ENABLE)
                .eq(null != type, Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime);

        return categoryMapper.selectList(wrapper);
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 转换为 entity 数据库对象
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }
}
